package br.com.werp.regras.endereco.integracao;

import br.com.werp.regras.geral.integracao.MasterDAO;
import br.com.werp.regras.geral.util.Constantes;
import br.com.werp.regras.geral.util.Util;
import br.com.werp.regras.geral.vos.endereco.CepFormVO;
import br.com.werp.regras.geral.vos.tabelas.Cep;

import java.sql.Connection;

import java.util.List;
import java.util.ResourceBundle;


/**
 * @author Rafael Carneiro - <a href = "mailto:rafael@portaljava.com">
 * rafael@portaljava.com</a>
 * Criado em: 02/05/2005
 *
 */
public class CepDAO extends MasterDAO {
    private static CepDAO cepDAO;

    private CepDAO(String banco) throws Exception {
        super(banco);
    }

    public static CepDAO getCepDAO() throws Exception {
        if (cepDAO == null) {
            ResourceBundle bundle = ResourceBundle.getBundle(Constantes.CONFIG);
            cepDAO = new CepDAO(bundle.getString(Constantes.BANCO));
        }

        return cepDAO;
    }

    public List pesquisarCepVO(CepFormVO cepForm) throws Exception {
        Connection connection = getConexao();

        try {
            String tabelas = "cep,cidade,bairro,logradouro,uf";
            String where = "and ceps_bairro = bair_id and ceps_cidade = cida_id and ceps_logradouro = logr_id and cida_uf = unfd_id ";
            String[] excluidas = {
                    "unfd_id", "unfd_uf", "unfd_pais", "pais_descricao",
                    "pais_id"
                };

            if (!Util.isNullOrEmpty(cepForm.getBair_descricao())) {
                where += (" and bair_descricao like " +
                aspasLike(cepForm.getBair_descricao()));
                cepForm.setBair_descricao(null);
            }

            if (!Util.isNullOrEmpty(cepForm.getCida_descricao())) {
                where += (" and cida_descricao like " +
                aspasLike(cepForm.getCida_descricao()));
                cepForm.setCida_descricao(null);
            }

            if (!Util.isNullOrEmpty(cepForm.getLogr_descricao())) {
                where += (" and logr_descricao like " +
                aspasLike(cepForm.getLogr_descricao()));
                cepForm.setLogr_descricao(null);
            }

            return pesquisarVO(connection, cepForm, where, tabelas, excluidas);
        } finally {
            liberaConeccao(connection);
        }
    }

    public void salvarCep(Cep cep, Object key) throws Exception {
        Connection connection = validarTransacao(key);
        inserirVO(connection, cep, new String[] { "ceps_id" });
    }

    public void atualizarCep(Cep cep, Object key) throws Exception {
        Connection connection = validarTransacao(key);
        atualizarVO(connection, cep, new String[] { "ceps_id" });
    }

    public void excluirCep(Cep cep, Object key) throws Exception {
        Connection connection = validarTransacao(key);
        excluirVO(connection, cep);
    }

    public List pesquisarCep(Cep cep) throws Exception {
        Connection connection = getConexao();

        try {
            return pesquisarVO(connection, cep);
        } finally {
            liberaConeccao(connection);
        }
    }
}
